define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');

    $.widget('rk.pageHeadCtrl', {
        _create: function(){
            var me = this;
            var elem = me.element;            
        },
        _init: function(){
            var me = this;
            var elem = me.element;

            me.globalSearch = elem.find('#globalSearch').pageSearchBarCtrl();
            me.globalNotice = elem.find('#globalNotice').pageNoticeBtnCtrl();
            me.oaMenuBar = elem.find('#oamenubar').pageOAMenuCtrl();

            me._renewNoticeNum();
            $.registLongPulling(function(msg) {
                if(msg.k == "newnotice" || msg.k == "remind" || msg.k == "at" || msg.k == "announcement" || msg.k == "notice") {
                    var nowPanel = rk.oa.getPanel();
                    var nowPanels = nowPanel.find("[act='notice_panel_for_renew_count']");
                    if(!nowPanel || nowPanels.length == 0){
                        me._renewNoticeNum();
                    }
                }
                if (msg.k === "inbox") {
                    me._handlerPrivateMsg(msg);
                }
            });
            var oaTogger = $('#oa_panel_toggler');
            oaTogger.click(function(){
                var isOpen = rk.oa.panelIsOpen();
                var isExist = rk.oa.panelExist();

                if(isOpen){                    
                    rk.oa.closeCurtain();
                    rk.oa.closeContent();
                    rk.oa.hidePanel(function(){
                        //oaTogger.find('.oa-slidebar-taggle').addClass('oa-slidebar-taggle-open');
                    });
                }else if(isExist){
                    rk.oa.showPanel(function(){
                        //oaTogger.find('.oa-slidebar-taggle').removeClass('oa-slidebar-taggle-open');
                    });
                }else{
                    $('#globalNotice').click();
                }
            });
            elem.find('[title]').beTip({dir: 'left'});

            elem.find('.function-btn').mousedown(function(e){e.stopPropagation()});//防止干扰crm的详情页滑出
        },
        _renewNoticeNum:function(){
            var me = this;
            var elem = me.element;
            var path = location.pathname.toLowerCase();
            if(path.indexOf("noticeatme.action")==-1) {
                rk.post("/json/oa_notice/unRead-counts.action?a=" + Math.random(), {}, function (json) {
                    if (json.status === 0) {
                        var data = json.unRead_counts;
                        var nums = data.remindUnreadCount + data.noticeUnreadCount + data.atmeUnreadCount + data.annoUnreadCount;
                        if (nums - 0 > 99) {
                            nums = "99+";
                        }
                        me.globalNotice.pageNoticeBtnCtrl('setNum', nums);
                    } else {
                        me.globalNotice.pageNoticeBtnCtrl('setNum', 0);
                    }
                });
            }
        },
        _handlerPrivateMsg: function(msg){
            //处理私信
            var badgeCount = 0;
            var pmId = null;
            try {
                pmId = $('#privatemsg').find(".pg-pm-messagebox").privateMsgDetailCtrl("currentId");
            } catch (e) {
                pmId = null;
            }
            if (pmId == msg.d) {
                //加载数据到当前打开窗口
                $.getJSON("/json/sns_wx/detail.action", {
                    privateMsgId : msg.d,
                    pageNo : 1,
                    pageSize : 1
                }).done(function(json) {
                    if (json.status) {
                        rk.noticeError(json.status);
                        return;
                    }
                    if (json.data) {
                        $('#privatemsg').find(".pg-pm-messagebox").privateMsgDetailCtrl("appendData", json.data);
                        if($('#privaemsg').data('msgInited') === 'privatemsginited'){
                            $('#privatemsg').privateMsgCtrl("reorderList", msg.d);
                        }else{
                            $('#privatemsg').privateMsgCtrl();
                            $('#privatemsg').privateMsgCtrl("reorderList", msg.d);
                        }
                    }
                }).fail(function(def, code, message) {
                    rk.noticeError(rk.i18n('REQUEST_ERROR'));
                });
            } else {
                //其他情况，需要把badge +1
                badgeCount++;
            }
            if (badgeCount > 0) {
                if($('#privatemsg').find('.message-tab').find('ul.pm-people-list').length > 0){
                    $('#privatemsg').privateMsgCtrl("reorderList", msg.d, badgeCount);
                }
                $('#privatemsg').privateMsgCtrl("initUnRead");
            }
        },
        setTitle:function(title, isHtml){
            var me = this;
            var elem = me.element;
            if(typeof isHtml == 'undefined') isHtml = false;//直接指定链接地址
            if(isHtml){
                elem.find('>.xsy_oa_detail_crumbs ul.xsy_oa_detail_path').html(title);
                return;
            }
            if(typeof title == 'string' || $.isNumeric(title)){
                elem.find('>.xsy_oa_detail_crumbs ul.xsy_oa_detail_path').html('<li>'+title+'</li>');
            }else if($.isArray(title)){
                var html = '';
                for(var i=0,len=title.length;i<len;i++){
                    var o = title[i];
                    var tpl;
                    if(o.href){
                        tpl = '<li><a href="{{href}}">{{title}}</a></li>'
                    }else{
                        tpl = '<li>{{title}}</li>';
                        if(typeof o == 'string' || $.isNumeric(o)) o = {title: o};
                    }
                    html += rk.templateText(tpl, o);
                }
                elem.find('>.xsy_oa_detail_crumbs ul.xsy_oa_detail_path').html(html);
            }
        }
});
});